﻿var Sk = {
    Domain: { Cart: 'http://cart.silenthink.com', Item: 'http://item.silenthink.com', Passport: 'http://passport.silenthink.com', My: 'http://my.silenthink.com' },
    CurrentUser: { IsAuthenticated: false, NickName: '' },
    Cart: {
        RenderMiniCart: function () {
            var url = Sk.Domain.Cart + "/mini/callback=?";
            $.getJSON(url, function (result) {
                if (result.Status == true) {
                    var amount = result.Data.Quantity;
                    $("#cart-amount").text(amount);
                }
            });
        }
    },
    Item: {

    },
    Passport: {
        Fun: {
            login: function (form, win) {
                var username = form.username.value;
                var password = form.password.value;
                var captcha = form.captcha.value;
                
                var param = $(form).serialize();
                $.postJSON(form.action, param, function (result) {
                    if (result.Status) {
                        //console.log(Sk.CurrentUser);
                        win.Sk.CurrentUser.IsAuthenticated = true;
                        win.Sk.CurrentUser.NickName = result.Data;
                        win.$.fancybox.close();
                        if (result.Extra != null && result.Extra != "") {
                            win.location = result.Extra;
                        }
                    }
                    else {
                        $("#login-message").html(result.Message);
                        //window.setTimeout(function () { $("#login-message").html(''); }, 2000);
                    }
                    //console.log(Sk.CurrentUser);
                });
                //$.fancybox.open({ href: Sk.Domain.Passport + '/home/login/0', type: 'iframe', padding: 5, width: 700, height: 500 });
                return false;
            },
            loginCallback: function (result) {
                if (result.Status) {
                    window.top.Sk.CurrentUser.IsAuthenticated = true;
                    window.top.Sk.CurrentUser.NickName = result.Data;
                    window.top.$.fancybox.close();
                    if (result.Extra != null && result.Extra != "") {
                        window.top.location = result.Extra;
                    }
                }
                else {
                    $("#login-message").html(result.Message);
                    window.setTimeout(function () { $("#login-message").html(''); }, 2000);
                }
            },
            validateLogin: function (win) {
                $('#login-form').validate({
                    rules: { 'username': { required: true, email: true }, 'password': { required: true, password: false }, 'captcha': { required: true, rangelength: [4, 4]} },
                    messages: { 'username': { required: '请输入邮箱！', email: '请输入邮箱！' }, password: { required: '请输入密码！', password: '请输入密码！' }, 'captcha': { required: '请输入验证码!', rangelength: '请输入验证码!'} },
                    submitHandler: function (form) {
                        var param = $(form).serialize();
                        $.postJSON(form.action, param, function (result) {
                            if (result.Status) {
                                //console.log(Sk.CurrentUser);
                                win.Sk.CurrentUser.IsAuthenticated = true;
                                win.Sk.CurrentUser.NickName = result.Data;
                                win.$.fancybox.close();
                                if (result.Extra != null && result.Extra != "") {
                                    win.location = result.Extra;
                                }
                            }
                            else {
                                $("#login-message").html(result.Message);
                                window.setTimeout(function () { $("#login-message").html(''); }, 2000);
                            }
                            //console.log(Sk.CurrentUser);
                        });
                        return false;
                    }
                });
            }
        }
    },
    My: {
        Fun: {
            avatar: function () {
                $.fancybox.open({ href: Sk.Domain.My + '/home/avatar?r=' + Math.random(), type: 'ajax', title: '修改头像' });
            }
        }
    },
    Auction: {
        Fun: {
            buy: function (id, title) {
                if (Sk.CurrentUser.IsAuthenticated) {
                    Sk.Passport.Fun.login();
                }
                else {
                    $.fancybox.open({ href: '/home/buy/' + id + '?r=' + Math.random(), type: 'ajax', title: title });
                }
                return false;
            }
        }
    },
    Util: { isPassword: function (str) {
        //这个正则要求密码长度最少6位，包含至少1个特殊字符，2个数字，2个大写字母和一些小写字母
        //分解
        //(?=^.{12,25}$) -- 密码长度12-25，自己改变数字可以调节  
        //(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1}) -- 至少一个特殊字母
        //(?=(?:.*?\d){2}) -- 至少2个数字
        //(?=.*[a-z]) -- a-z的小写字母  
        //(?=(?:.*?[A-Z]){2}) -- 至少2个大写字母 
        var reg = /^(?=^.{6,18}$)(?=(?:.*?\d){2})(?=.*[a-z])(?=(?:.*?[A-Z]){2})(?=(?:.*?[!@#$%*()_+^&}{:;?.]){1})(?!.*\s)[0-9a-zA-Z!@#$%*()_+^&]*$/;
        if (reg.test(str)) {
            return true;
        }
        return false;
    }
    }
};

